package com.ara.common.kernel.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;

import com.ara.common.kernel.base.BaseDialog;
import com.ara.common.kernel.databinding.DialogXPromptBinding;


/**
 * Created by XieXin on 2018/12/26.
 * 提示对话框
 */
public class XPromptDialog extends BaseDialog<DialogXPromptBinding> {
    private final Builder builder;

    private XPromptDialog(@NonNull Context context, Lifecycle lifecycle, @NonNull Builder builder) {
        super(context, lifecycle);
        this.builder = builder;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(binding.getRoot());

        initView();
    }

    private void initView() {
        if (binding == null) return;
        if (!TextUtils.isEmpty(builder.title)) binding.tvTitle.setText(builder.title);
        if (builder.titleResId > 0) binding.tvTitle.setText(builder.titleResId);
        if (builder.titleColor != 0) binding.tvTitle.setTextColor(builder.titleColor);
        if (builder.titleColorResId > 0)
            binding.tvTitle.setTextColor(ContextCompat.getColor(builder.context, builder.titleColorResId));

        if (!TextUtils.isEmpty(builder.content)) binding.tvContent.setText(builder.content);
        if (builder.contentResId > 0) binding.tvContent.setText(builder.contentResId);
        if (builder.contentColor != 0) binding.tvContent.setTextColor(builder.contentColor);
        if (builder.contentColorResId > 0)
            binding.tvContent.setTextColor(ContextCompat.getColor(builder.context, builder.contentColorResId));

        if (!TextUtils.isEmpty(builder.textPositive))
            binding.tvPositive.setText(builder.textPositive);
        if (builder.textPositiveResId > 0) binding.tvPositive.setText(builder.textPositiveResId);
        if (builder.textPositiveColor != 0)
            binding.tvPositive.setTextColor(builder.textPositiveColor);
        if (builder.textPositiveColorResId > 0)
            binding.tvPositive.setTextColor(ContextCompat.getColor(builder.context, builder.textPositiveColorResId));

        if (!TextUtils.isEmpty(builder.textNegative))
            binding.tvNegative.setText(builder.textNegative);
        if (builder.textNegativeResId > 0) binding.tvNegative.setText(builder.textNegativeResId);
        if (builder.textNegativeColor != 0)
            binding.tvNegative.setTextColor(builder.textNegativeColor);
        if (builder.textNegativeColorResId > 0)
            binding.tvNegative.setTextColor(ContextCompat.getColor(builder.context, builder.textNegativeColorResId));

        if (!TextUtils.isEmpty(builder.textNegative) || builder.textNegativeResId > 0) {
            binding.tvNegative.setVisibility(View.VISIBLE);
            binding.viewLine.setVisibility(View.VISIBLE);
        }


        binding.tvPositive.setOnClickListener(v -> {
            if (builder.isDismiss) dismiss();
            if (builder.onPositive != null)
                builder.onPositive.onXClickListener(XPromptDialog.this);
        });
        if (binding.tvNegative.getVisibility() == View.VISIBLE)
            binding.tvNegative.setOnClickListener(v -> {
                if (builder.isDismiss) dismiss();
                if (builder.onNegative != null)
                    builder.onNegative.onXClickListener(XPromptDialog.this);
            });


        if (!builder.isShowButton) {
            binding.tvNegative.setVisibility(View.GONE);
            binding.tvPositive.setVisibility(View.GONE);
        }

        setDialogWidth(getWindow(), getContext());

        setCancelable(builder.cancelable);
    }

    @Override
    public void show() {
        initView();
        super.show();
    }

    public void setTitle(String title) {
        this.builder.title(title);
    }

    public void setTitle(@StringRes int resId) {
        this.builder.title(resId);
    }

    public void setContent(String content) {
        this.builder.content(content);
    }

    public void setContent(@StringRes int resId) {
        this.builder.content(resId);
    }

    public void setOnPositive(XClickListener onPositive) {
        this.builder.onPositive(onPositive);
    }

    public void setOnNegative(XClickListener onNegative) {
        this.builder.onNegative(onNegative);
    }

    public void setShowButton(boolean isShowButton) {
        this.builder.showButton(isShowButton);
    }

    public static class Builder {
        private Context context;
        private Lifecycle lifecycle;
        private String title;
        private int titleResId;
        private String content;
        private int contentResId;
        private String textPositive;
        private int textPositiveResId;
        private String textNegative;
        private int textNegativeResId;
        private int titleColor;
        private int titleColorResId;
        private int contentColor;
        private int contentColorResId;
        private int textPositiveColor;
        private int textPositiveColorResId;
        private int textNegativeColor;
        private int textNegativeColorResId;
        private boolean cancelable;
        private boolean isShowButton = true;
        private boolean isDismiss = true;
        private XClickListener onPositive;
        private XClickListener onNegative;

        public Builder(@NonNull Context context) {
            this.context = context;
        }

        public Builder bindLifecycle(Lifecycle lifecycle) {
            this.lifecycle = lifecycle;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            this.titleResId = 0;
            return this;
        }

        public Builder title(@StringRes int resId) {
            this.titleResId = resId;
            this.title = "";
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            this.contentResId = 0;
            return this;
        }

        public Builder content(@StringRes int resId) {
            this.contentResId = resId;
            this.content = "";
            return this;
        }

        public Builder textPositive(String textPositive) {
            this.textPositive = textPositive;
            this.textPositiveResId = 0;
            return this;
        }

        public Builder textPositive(@StringRes int resId) {
            this.textPositiveResId = resId;
            this.textPositive = "";
            return this;
        }

        public Builder textNegative(String textNegative) {
            this.textNegative = textNegative;
            this.textNegativeResId = 0;
            return this;
        }

        public Builder textNegative(@StringRes int resId) {
            this.textNegativeResId = resId;
            this.textNegative = "";
            return this;
        }

        public Builder titleColor(@ColorInt int titleColor) {
            this.titleColor = titleColor;
            return this;
        }

        public Builder titleColorResource(@ColorRes int resId) {
            this.titleColorResId = resId;
            return this;
        }

        public Builder contentColor(@ColorInt int contentColor) {
            this.contentColor = contentColor;
            return this;
        }

        public Builder contentColorResource(@ColorRes int resId) {
            this.contentColorResId = resId;
            return this;
        }

        public Builder textPositiveColor(@ColorInt int textPositiveColor) {
            this.textPositiveColor = textPositiveColor;
            return this;
        }

        public Builder textPositiveColorResource(@ColorRes int resId) {
            this.textPositiveColorResId = resId;
            return this;
        }

        public Builder textNegativeColor(@ColorInt int textNegativeColor) {
            this.textNegativeColor = textNegativeColor;
            return this;
        }

        public Builder textNegativeColorResource(@ColorRes int resId) {
            this.textNegativeColorResId = resId;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder showButton(boolean isShowButton) {
            this.isShowButton = isShowButton;
            return this;
        }

        public Builder dismiss(boolean isDismiss) {
            this.isDismiss = isDismiss;
            return this;
        }

        public Builder onPositive(XClickListener onPositive) {
            this.onPositive = onPositive;
            return this;
        }

        public Builder onNegative(XClickListener onNegative) {
            this.onNegative = onNegative;
            return this;
        }

        public XPromptDialog build() {
            return new XPromptDialog(context, lifecycle, this);
        }

        public XPromptDialog show() {
            XPromptDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }

    public interface XClickListener {
        void onXClickListener(XPromptDialog dialog);
    }
}
